热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

使用PHP连接到远程MySQL服务器-ConnectingtoremoteMySQLserverusingPHP

IamattemptingtoconnecttoaremoteMySQLserverfrommylocalmachinevirtualhostusingthefoll

I am attempting to connect to a remote MySQL server from my local machine virtualhost using the following code:

我正在尝试使用以下代码从本地计算机虚拟主机连接到远程MySQL服务器:

$cOnn= mysql_connect("$dbhost", "$dbuser", "$dbpass") or die(mysql_error());
        mysql_select_db($dbname, $conn) or die(mysql_error());

My problem is that I am unable to connect locally, receiving the error:

我的问题是我无法在本地连接,收到错误:

Can't connect to MySQL server on 'xxx.xxx.xxx.xxx' (10060)

无法连接到'xxx.xxx.xxx.xxx'上的MySQL服务器(10060)

This is not the case when I upload the same PHP file to the server. I am able to query the database with no problems at all.

当我将相同的PHP文件上传到服务器时,情况并非如此。我能够毫无问题地查询数据库。

I am unable to connect via command line either, but I can access cPanel which rules out the chance of my IP being banned accidentally.

我也无法通过命令行连接,但我可以访问cPanel,它排除了我的IP被意外禁止的可能性。

My local server is running PHP 5.2.9, the remote server 5.2.12

我的本地服务器运行PHP 5.2.9,远程服务器5.2.12

4 个解决方案

#1


17  

  • firewall of the server must be set-up to enable incomming connections on port 3306
  • 必须设置服务器的防火墙以启用端口3306上的连接
  • you must have a user in MySQL who is allowed to connect from % (any host) (see manual for details)
  • 您必须在MySQL中拥有一个允许从%(任何主机)连接的用户(有关详细信息,请参阅手册)

The current problem is the first one, but right after you resolve it you will likely get the second one.

目前的问题是第一个问题,但在您解决问题之后,您可能会得到第二个问题。

#2


3  

It is very easy to connect remote MySQL Server Using PHP, what you have to do is:

使用PHP连接远程MySQL服务器非常容易,您需要做的是:

  1. Create a MySQL User in remote server.

    在远程服务器中创建MySQL用户。

  2. Give Full privilege to the User.

    为用户授予完全权限。

  3. Connect to the Server using PHP Code (Sample Given Below)

    使用PHP代码连接到服务器(下面给出的示例)

$link = mysql_connect('your_my_sql_servername or IP Address', 'new_user_which_u_created', 'password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}

echo 'Connected successfully';

mysql_select_db('sandsbtob',$link) or die ("could not open db".mysql_error());
// we connect to localhost at port 3306

#3


2  

I just solved this kind of a problem. What I've learned is:

我刚刚解决了这个问题。我学到的是:

  1. you'll have to edit the my.cnf and set the bind-address = your.mysql.server.address under [mysqld]
  2. 你必须编辑my.cnf并在[mysqld]下设置bind-address = your.mysql.server.address
  3. comment out skip-networking field
  4. 评论跳过网络领域
  5. restart mysqld
  6. 重启mysqld
  7. check if it's running

    检查它是否正在运行

    mysql -u root -h your.mysql.server.address –p 
    
  8. create a user (usr or anything) with % as domain and grant her access to the database in question.

    使用%作为域创建用户(usr或任何东西)并授予她对相关数据库的访问权限。

    mysql> CREATE USER 'usr'@'%' IDENTIFIED BY 'some_pass';
    mysql> GRANT ALL PRIVILEGES ON testDb.* TO 'monty'@'%' WITH GRANT OPTION;
    
  9. open firewall for port 3306 (you can use iptables. make sure to open port for eithe reveryone, or if you're in tight securety, then only allow the client address)

    打开端口3306的防火墙(你可以使用iptables。确保打开端口以便进行复制,或者如果你的安全性很高,那么只允许客户端地址)

  10. restart firewall/iptables
  11. 重启firewall / iptables

you should be able to now connect mysql server form your client server php script.

你应该能够现在连接mysql服务器从客户端服务器PHP脚本。

#4


0  

This maybe not the answer to poster's question.But this may helpful to people whose face same situation with me:

这可能不是海报问题的答案。但这可能对那些与我面临同样情况的人有所帮助:

The client have two network cards,a wireless one and a normal one. The ping to server can be succeed.However telnet serverAddress 3306 would fail. And would complain

客户端有两个网卡,一个是无线网卡,另一个是普通网卡。 ping到服务器可以成功。但telnet serverAddress 3306将失败。并会抱怨

Can't connect to MySQL server on 'xxx.xxx.xxx.xxx' (10060)

无法连接到'xxx.xxx.xxx.xxx'上的MySQL服务器(10060)

when try to connect to server.So I forbidden the normal network adapters. And tried telnet serverAddress 3306 it works.And then it work when connect to MySQL server.

当试图连接到服务器。所以我禁止正常的网络适配器。并尝试telnet serverAddress 3306它的工作原理。然后它连接到MySQL服务器时工作。


推荐阅读
  • http:my.oschina.netleejun2005blog136820刚看到群里又有同学在说HTTP协议下的Get请求参数长度是有大小限制的,最大不能超过XX ... [详细]
  • 本文介绍了Web学习历程记录中关于Tomcat的基本概念和配置。首先解释了Web静态Web资源和动态Web资源的概念,以及C/S架构和B/S架构的区别。然后介绍了常见的Web服务器,包括Weblogic、WebSphere和Tomcat。接着详细讲解了Tomcat的虚拟主机、web应用和虚拟路径映射的概念和配置过程。最后简要介绍了http协议的作用。本文内容详实,适合初学者了解Tomcat的基础知识。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • Linux重启网络命令实例及关机和重启示例教程
    本文介绍了Linux系统中重启网络命令的实例,以及使用不同方式关机和重启系统的示例教程。包括使用图形界面和控制台访问系统的方法,以及使用shutdown命令进行系统关机和重启的句法和用法。 ... [详细]
  • 阿,里,云,物,联网,net,core,客户端,czgl,aliiotclient, ... [详细]
  • ZSI.generate.Wsdl2PythonError: unsupported local simpleType restriction ... [详细]
  • 本文介绍了计算机网络的定义和通信流程,包括客户端编译文件、二进制转换、三层路由设备等。同时,还介绍了计算机网络中常用的关键词,如MAC地址和IP地址。 ... [详细]
  • 本文介绍了通过ABAP开发往外网发邮件的需求,并提供了配置和代码整理的资料。其中包括了配置SAP邮件服务器的步骤和ABAP写发送邮件代码的过程。通过RZ10配置参数和icm/server_port_1的设定,可以实现向Sap User和外部邮件发送邮件的功能。希望对需要的开发人员有帮助。摘要长度:184字。 ... [详细]
  • 自动轮播,反转播放的ViewPagerAdapter的使用方法和效果展示
    本文介绍了如何使用自动轮播、反转播放的ViewPagerAdapter,并展示了其效果。该ViewPagerAdapter支持无限循环、触摸暂停、切换缩放等功能。同时提供了使用GIF.gif的示例和github地址。通过LoopFragmentPagerAdapter类的getActualCount、getActualItem和getActualPagerTitle方法可以实现自定义的循环效果和标题展示。 ... [详细]
  • 本文介绍了如何使用C#制作Java+Mysql+Tomcat环境安装程序,实现一键式安装。通过将JDK、Mysql、Tomcat三者制作成一个安装包,解决了客户在安装软件时的复杂配置和繁琐问题,便于管理软件版本和系统集成。具体步骤包括配置JDK环境变量和安装Mysql服务,其中使用了MySQL Server 5.5社区版和my.ini文件。安装方法为通过命令行将目录转到mysql的bin目录下,执行mysqld --install MySQL5命令。 ... [详细]
  • Java在运行已编译完成的类时,是通过java虚拟机来装载和执行的,java虚拟机通过操作系统命令JAVA_HOMEbinjava–option来启 ... [详细]
  • 本文介绍了在Linux下安装和配置Kafka的方法,包括安装JDK、下载和解压Kafka、配置Kafka的参数,以及配置Kafka的日志目录、服务器IP和日志存放路径等。同时还提供了单机配置部署的方法和zookeeper地址和端口的配置。通过实操成功的案例,帮助读者快速完成Kafka的安装和配置。 ... [详细]
  • 本文介绍了Hyperledger Fabric外部链码构建与运行的相关知识,包括在Hyperledger Fabric 2.0版本之前链码构建和运行的困难性,外部构建模式的实现原理以及外部构建和运行API的使用方法。通过本文的介绍,读者可以了解到如何利用外部构建和运行的方式来实现链码的构建和运行,并且不再受限于特定的语言和部署环境。 ... [详细]
  • imx6ull开发板驱动MT7601U无线网卡的方法和步骤详解
    本文详细介绍了在imx6ull开发板上驱动MT7601U无线网卡的方法和步骤。首先介绍了开发环境和硬件平台,然后说明了MT7601U驱动已经集成在linux内核的linux-4.x.x/drivers/net/wireless/mediatek/mt7601u文件中。接着介绍了移植mt7601u驱动的过程,包括编译内核和配置设备驱动。最后,列举了关键词和相关信息供读者参考。 ... [详细]
  • 本文介绍了南邮ctf-web的writeup,包括签到题和md5 collision。在CTF比赛和渗透测试中,可以通过查看源代码、代码注释、页面隐藏元素、超链接和HTTP响应头部来寻找flag或提示信息。利用PHP弱类型,可以发现md5('QNKCDZO')='0e830400451993494058024219903391'和md5('240610708')='0e462097431906509019562988736854'。 ... [详细]
author-avatar
Xiao兔兔乖乖
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有